<?php
class UserModel extends Model{
	protected $table='kq_user';
	protected $pk='uid';
	//表中的字段
	protected $fields=array('uid','uname','paswd','logtime','real_name','mobile_no',
		'logip','status','tno');
	//自动验证规则
	protected $rules=array(
			array('uname',1,'用户名不能为空','require'),
			array('paswd',1,'密码不能为空','require'),
			
		);
	//自动填充规则
	protected $leftfields=array(
		    array('status','value','1'),
			array('logtime','function','time')
		);

	
	protected function encPasswd($passwd){
		return md5($passwd);
	}


	public function findRoles($offset=0,$limit=10){
    $sql="SELECT kq_user.uname, kq_user.status,kq_role_user.user_id, kq_role.name FROM kq_user,kq_role_user,kq_role WHERE kq_user.uid=kq_role_user.user_id AND kq_role_user.role_id=kq_role.role_id.' limit '.$offset.','.$limit";
    return $this->db->getAll($sql);
   }

    public function findAllRoles(){
    $sql="SELECT kq_user.uname, kq_user.status,kq_user.real_name,kq_user.mobile_no,kq_role_user.user_id, kq_role.name FROM kq_user,kq_role_user,kq_role WHERE kq_user.uid=kq_role_user.user_id AND kq_role_user.role_id=kq_role.role_id order by kq_user.uid";
    return $this->db->getAll($sql);
   }
   public function checkUser($tno,$passwd=''){

	    //根据用户名和密码查询
        
	  	$sql="select uid,tno,uname,real_name,paswd,logtime,logip from ".$this->table." where tno='".
	  	$tno."'";
	  	$row=$this->db->getRow($sql);
	  	if(empty($row)){
	  		return false;
	  	}
	  	if($row['paswd']!=$this->encPasswd($passwd)){
	  		return false;
	  	}
	  	unset($row['paswd']);
	  	return $row;
	}

	public function findAllUname(){
		$sql = "select uname from".$this->table;
		return $this->db->getAll($sql);
	}
}
?>